<?php

namespace App\Jobs\Passenger;

use App\Models\Passengers\Passenger;
use App\Models\Passengers\Repositories\Interfaces\PassengerRepositoryInterface;
use App\Trip\Passengers\PassengerCheck;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class ProcessPassengerGoCheck implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private PassengerCheck $check;
    private Passenger      $passenger;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Passenger $passenger, PassengerCheck $check)
    {
        $this->check     = $check;
        $this->passenger = $passenger;
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws GuzzleException
     */
    public function handle()
    {
        $res = $this->check->goCheck();
        if ($res > 0) {
            /**
             * @var PassengerRepositoryInterface $passengerRepo
             */
            $passengerRepo = app(PassengerRepositoryInterface::class);
            $passengerRepo->updateVerifyState($this->passenger, $res);
        }
    }


    /**
     * 任务的唯一 ID。
     *
     * @return string
     */
    public function uniqueId()
    {
        return $this->check->getIdentityCode();
    }

    public function failed()
    {
        $identity_code = $this->check->getIdentityCode();
        Log::error("证件号姓名检测失败：{$identity_code}");
    }


    /**
     * 计算重试任务之前要等待的秒数。
     *
     * @return array
     */
    public function backoff()
    {
        return [5, 30, 60];
    }
}
